package com.dhecp.project.storage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.common.service.UpdateLogService;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.storage.domain.StockControl;
import com.dhecp.project.storage.mapper.StockControlMapper;
import com.dhecp.project.storage.service.StockControlService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * @program: kjecp
 * @description: 仓储-库存管理
 * @author: hlf
 * @create: 2022-09-01 16:04
 */
@Service
public class StockControlServiceImpl extends BaseModuleServiceImpl implements StockControlService {

    @Autowired
    private StockControlMapper storageMapper;
    @Autowired
    private UpdateLogService updateLogService;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    /**
     *@Description: 查询
     *@Param: [params]
     *@return: java.util.List<com.dhecp.project.storage.domain.StockControl>
     *@Author: hlf
     *@date: 2022-05-09 10:10
     */
    @Override
    public List<StockControl> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
		if (params.get("field") == null || params.get("field") == ""){
			String orderBy = "ProductCode asc";
			PageUtils.startPageOrder(orderBy);
		}else{
			PageUtils.startPage();
		}
        return storageMapper.select(params);
    }

    /**
     * @Description: 查询指定单nid的首记录
     * @Param: [params]
     * @return: com.dhecp.project.storage.domain.StockControl
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @Override
    public StockControl findByGuid(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        return storageMapper.findById(params);
    }

    /**
     * @Description: 查询指定单nid的首记录
     */
    @Override
    public StockControl findByGuid(String guid) {
        return storageMapper.findByGuid(guid);
    }


    /**
     * @Description: 保存
     * @Param: [params] moduleTable 必要参数：功能唯一标识
     * @Param: [params] infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     * @Param: [params] infoItems 必要参数：对应实体类的json格式字符串
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @Override
    public AjaxResult saveData(Map<String, Object> params) {

        StockControl oldData = new StockControl();
        StockControl stockControl = JSON.parseObject(params.get("infoItems").toString(), StockControl.class);

        Map<String, Object> params2 = new HashMap<String, Object>();
        params2.put("guid",stockControl.getGuid());
        oldData = storageMapper.findById(params2);

        if (stockControl.getGuid() != null && !stockControl.getGuid().equals("")){
            storageMapper.update(stockControl);
            updateLogService.addUpdateLog(params.get("moduleTable").toString(),stockControl.getGuid(),oldData,stockControl,"修改");
        }

        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), stockControl.getGuid());
    }

}
